package com.yanqu.road.server.cmd.activity.autoreward.rank;

import com.yanqu.road.entity.autodata.chuhan.data.CrossChuHanRank;
import com.yanqu.road.entity.autodata.chuhan.data.CrossChuHanRankListModel;
import com.yanqu.road.entity.autodata.chuhan.join.CrossChuHanUnionJoin;
import com.yanqu.road.entity.autodata.chuhan.join.CrossChuHanUnionJoinMember;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.pb.activity.AutoSendRewardProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.manager.activity.chuhan.CrossChuHanMgr;
import com.yanqu.road.server.manager.activity.chuhan.activity.CrossChuHanActivity;

import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChuHanActivityRank extends BaseCrossRank {

    public ChuHanActivityRank(int activityId) {
        super(activityId);
    }

    @Override
    public AutoSendRewardProto.CrossUserRankMsg.Builder getCrossUserRankMsg() {
        return getCrossUserRankMsg(UnionActivityGroupHelper.NO_NEED_GROUP_ID);
    }

    @Override
    public AutoSendRewardProto.CrossUnionRankMsg.Builder getCrossUnionRankMsg() {
        return getCrossUnionRankMsg(UnionActivityGroupHelper.NO_NEED_GROUP_ID);
    }


    private void addCampUnionRankData(AutoSendRewardProto.CrossUnionRankMsg.Builder msg, int conditionType, int campRankNum,
                                      String unionUid, int unionRankNum, Map<Long, CrossChuHanUnionJoinMember> memberMap) {

        AutoSendRewardProto.UnionRankMsg.Builder builder = AutoSendRewardProto.UnionRankMsg.newBuilder();
        builder.setConditionType(conditionType);
        builder.setUnionId(unionUid);
        builder.setRank(unionRankNum);
        builder.setCampRank(campRankNum);
        for (CrossChuHanUnionJoinMember member : memberMap.values()) {
            builder.addPosList(CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg.newBuilder().setUserId(member.getUserId()).setPos(member.getPosition()));
        }
        msg.addData(builder);
    }

    @Override
    public AutoSendRewardProto.CrossUserRankMsg.Builder getCrossUserRankMsg(int groupId) {
        CrossChuHanActivity activity = CrossChuHanMgr.getActivity(activityId, groupId);
        if (activity == null) {
            return null;
        }

        CrossChuHanRankListModel userRank = activity.getUserRank();
        LinkedList<CrossChuHanRank> rankList = userRank.getRankList();

        AutoSendRewardProto.CrossUserRankMsg.Builder msg = AutoSendRewardProto.CrossUserRankMsg.newBuilder();
        msg.setActivityId(activityId);
        int rankNum = 0;
        for (CrossChuHanRank rank : rankList) {
            rankNum++;
            long userId = Long.valueOf(rank.getId());
            addUserRankData(msg, eGamePlayerEventType.ChuHanUserZhanGongRank.getValue(), userId, rankNum);
        }
        return msg;
    }

    @Override
    public AutoSendRewardProto.CrossUnionRankMsg.Builder getCrossUnionRankMsg(int groupId) {
        CrossChuHanActivity activity = CrossChuHanMgr.getActivity(activityId, groupId);
        if (activity == null) {
            return null;
        }

        AutoSendRewardProto.CrossUnionRankMsg.Builder msg = AutoSendRewardProto.CrossUnionRankMsg.newBuilder();
        msg.setActivityId(activityId);

        // 拿阵营排名
        Map<Integer, Integer> campRankMap = new ConcurrentHashMap<>(); // 阵营，排名
        int rankNum = 0;
        LinkedList<CrossChuHanRank> campRankList = activity.getCampScoreRankList();
        for (CrossChuHanRank campRank : campRankList) {
            rankNum++;
            campRankMap.put(Integer.valueOf(campRank.getId()), rankNum);
        }

        // 拿商会榜
        Map<Integer, CrossChuHanRankListModel> campUnionRankMap = activity.getCampUnionRankMap(); // 阵营ID，商会榜
        Map<String, CrossChuHanUnionJoin> unionJoinMap = activity.getUnionJoinMap(); // 商会UID，商会报名记录
        for (Map.Entry<Integer, CrossChuHanRankListModel> entry : campUnionRankMap.entrySet()) {
            int campId = entry.getKey();
            int campRankNum = campRankMap.getOrDefault(campId, -1);
            int unionRankNum = 0;
            for (CrossChuHanRank unionRank : entry.getValue().getRankList()) {
                unionRankNum++;
                CrossChuHanUnionJoin join = unionJoinMap.get(unionRank.getId());
                if (join == null) {
                    continue;
                }
                addCampUnionRankData(msg, eGamePlayerEventType.ChuHanCampUnionZhanGongRank.getValue(), campRankNum,
                        unionRank.getId(), unionRankNum, join.getMemberMap());
            }

        }
        return msg;
    }
}
